import pytest
import torch
import sys
from pathlib import Path

# 确保使用绝对导入
sys.path.insert(0, str(Path(__file__).parent.parent))

# 统一使用src开头的绝对导入
from src.data.dataset import FontDataset
from src.models import StructEncoder, StyleEncoder, Discriminator

@pytest.fixture
def mock_config(tmp_path):
    data_dir = tmp_path / "data"
    data_dir.mkdir()
    
    # 创建符合要求的测试数据（包含预处理后的图像）
    for i in range(2):
        torch.save(
            (torch.rand(3, 128, 128),    # 已经预处理过的图像 [C,H,W]
             torch.randint(0, 99, (50,))),  # 路径序列
            data_dir / f"sample_{i}.pt"
        )
    
    return {
        'data_path': str(data_dir),
        'batch_size': 2,
        'epochs': 1,
        'lr_g': 2e-4,
        'lr_d': 2e-4
    }

@pytest.fixture
def test_device():
    return torch.device('cuda' if torch.cuda.is_available() else 'cpu')

@pytest.fixture
def mock_dataset():
    class MockDataset:
        def __init__(self):
            self.samples = [
                (torch.rand(3, 128, 128), torch.randint(0, 99, (4,))),
                (torch.rand(3, 128, 128), torch.randint(0, 99, (4,)))
            ]
        def __len__(self): return len(self.samples)
        def __getitem__(self, idx): return self.samples[idx]
    return MockDataset()